package com.service.business.member.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.beust.jcommander.internal.Lists;
import com.service.business.base.service.ConfigService;
import com.service.business.member.service.MemberService;
import com.service.business.member.service.MemberStarGradeConfigService;
import com.service.business.member.service.MemberStoreService;
import com.service.business.merchant.service.MerchantOrderService;
import com.service.dao.constants.ConfigConstants;
import com.service.dao.enums.CoinsEnum;
import com.service.dao.enums.StarGradeEnum;
import com.service.dao.mapper.MemberStarGradeConfigMapper;
import com.service.dao.model.Member;
import com.service.dao.model.MemberStarGradeConfig;
import com.service.dao.model.MemberStore;
import com.service.dao.model.SysConfig;
import com.service.dao.po.MemberCache;
import com.service.dao.util.StarGradeUtil;
import com.service.utility.common.MathUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;


@Service("memberStartGradeConfigService")
public class MemberStarGradeConfigServiceImpl extends ServiceImpl<MemberStarGradeConfigMapper, MemberStarGradeConfig> implements MemberStarGradeConfigService {

    @Autowired
    private MemberService memberService;
    @Autowired
    private ConfigService configService;
    @Autowired
    private MemberStoreService memberStoreService;
    @Autowired
    private MerchantOrderService merchantOrderService;

    @Override
    public Integer checkMemberStarGrade(MemberCache memberCache) {

        List<Integer> memberIdList = memberService.selectByPathAndStatus(memberCache.getMemberId()).stream()
                .map(Member::getId).collect(Collectors.toList());

        //获取团队返点业绩
        BigDecimal totalConcessionUsdt = ObjectUtils.isEmpty(memberIdList) ? BigDecimal.ZERO : merchantOrderService.getOfflineTotalConcessionUsdt(memberIdList);

        SysConfig config = configService.getConfigByName(ConfigConstants.OFFLINE_ORDER_GIFT_MEMBER_COINS);
        CoinsEnum coins = CoinsEnum.valueOf(config.getConfOptions());

        //自身挖矿积分
        MemberStore memberStore = memberStoreService.getMemberStore(memberCache.getMemberId(), coins.getName());
        Integer grade = memberCache.getStarGrade();
        MemberStarGradeConfig memberGrade = null;

        List<MemberStarGradeConfig> configList = listConfigById(memberCache.getStarGrade());
        for (MemberStarGradeConfig item : configList) {
            if (!checkCondition(item, totalConcessionUsdt, memberStore)) {
                memberGrade = item;
                break;
            }
        }

        if (ObjectUtils.isEmpty(memberGrade)) {
            return StarGradeUtil.isAdminModifyGrade(grade) ? StarGradeEnum.hundred_eleven.getValue() : StarGradeEnum.eleven.getValue();
        }

        if (isFirstGrade(memberGrade.getId())) {
            return memberGrade.getId();
        }

        if (StarGradeUtil.isAdminModifyGrade(grade) && memberGrade.getId() - 1 <= grade) {
            return grade;
        }

        return memberGrade.getId() - 1;
    }

    private boolean checkCondition(MemberStarGradeConfig item, BigDecimal totalConcessionUsdt, MemberStore memberStore) {

        boolean pass = totalConcessionUsdt.compareTo(item.getTeamRebateNum()) >= 0
                && memberStore.getNum().compareTo(item.getTotalMuNum()) >= 0;

        if (isFirstGrade(item.getId())) {
            return pass;
        }

        Integer childrenGrade = item.getId() - 1;
        List<Integer> starGradeList = Lists.newArrayList(
                childrenGrade,
                StarGradeUtil.isAdminModifyGrade(childrenGrade) ? childrenGrade - 100 : item.getId() + 100
        );
        int childrenGradeNum = memberService.countMemberByPidAndStarGrade(memberStore.getMemberId(), starGradeList);
        return pass && childrenGradeNum >= item.getChildrenGradeNum();
    }

    private boolean isFirstGrade(Integer startGrade) {
        return ObjectUtils.nullSafeEquals(startGrade, StarGradeEnum.one.getValue()) ||
                ObjectUtils.nullSafeEquals(startGrade, StarGradeEnum.hundred_one.getValue());
    }

    private List<MemberStarGradeConfig> listConfigById(Integer starGrade) {
        LambdaQueryWrapper<MemberStarGradeConfig> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (StarGradeUtil.isAdminModifyGrade(starGrade)) {
            lambdaQueryWrapper.ge(MemberStarGradeConfig::getId, StarGradeEnum.hundred_one.getValue());
        } else {
            lambdaQueryWrapper.le(MemberStarGradeConfig::getId, StarGradeEnum.twelve.getValue());
        }
        return list(lambdaQueryWrapper);
    }


}
